﻿/*
  In App.xaml:
  <Application.Resources>
      <vm:ViewModelLocatorTemplate xmlns:vm="using:ProjectForTemplates.ViewModel" x:Key="Locator" />
  </Application.Resources>
  
  In the View:
  DataContext="{Binding Source={StaticResource Locator}, Path=ViewModelName}"
*/

using System;
using System.Diagnostics.CodeAnalysis;
using com.IronOne.BoardPACWinApp.ViewModel.UserControlViewModel;
using com.IronOne.BoardPACWinAppService.Comment;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Ioc;
using GalaSoft.MvvmLight.Messaging;
using Microsoft.Practices.ServiceLocation;

namespace com.IronOne.BoardPACWinApp.ViewModel
{
    /// <summary>
    ///     This class contains static references to all the view models in the
    ///     application and provides an entry point for the bindings.
    ///     <para>
    ///         See http://www.galasoft.ch/mvvm
    ///     </para>
    /// </summary>
    public class ViewModelLocator
    {
        private static string _currentKey = Guid.NewGuid().ToString(); //Use to Identify cached viewmodels

        static ViewModelLocator()
        {
            ServiceLocator.SetLocatorProvider(() => SimpleIoc.Default);

            if (ViewModelBase.IsInDesignModeStatic)
            {
                //SimpleIoc.Default.Register<IDataService, Design.DesignDataService>();
            }
            else
            {
                SimpleIoc.Default.Register<ICommentService, CommentService>();
            }

            SimpleIoc.Default.Register<CommentViewModel>();
            SimpleIoc.Default.Register<ProfileUserControlViewModel>();
            SimpleIoc.Default.Register<AddCommentsViewModel>();
        }

        public static string CurrentKey
        {
            get { return _currentKey; }
            private set { _currentKey = value; }
        }

        [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public CommentViewModel Comments
        {
            get { return ServiceLocator.Current.GetInstance<CommentViewModel>(CurrentKey); }
        }

        [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public ProfileUserControlViewModel ProfileUserControl
        {
            get { return ServiceLocator.Current.GetInstance<ProfileUserControlViewModel>(CurrentKey); }
        }

        [SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic",
            Justification = "This non-static member is needed for data binding purposes.")]
        public AddCommentsViewModel AddComments
        {
            get { return ServiceLocator.Current.GetInstance<AddCommentsViewModel>(CurrentKey); }
        }

        /// <summary>
        ///     Cleans up all the resources.
        /// </summary>
        public static void Cleanup()
        {
            SimpleIoc.Default.Unregister<ProfileUserControlViewModel>(CurrentKey);
            SimpleIoc.Default.Unregister<CommentViewModel>(CurrentKey);
            SimpleIoc.Default.Unregister<AddCommentsViewModel>(CurrentKey);

            CurrentKey = Guid.NewGuid().ToString();
            Messenger.Reset();
        }

        public static void CleanupPartial()
        {
            SimpleIoc.Default.Unregister<ProfileUserControlViewModel>(CurrentKey);
            //CurrentKey = Guid.NewGuid().ToString();
        }
    }
}